<?php
namespace Common\Model;
use Think\Model;

class AreaModel extends Model{
	//ID转地区名称
	public function id2names($area_ids, $is_parent = false, $empty_name = ''){
		$area_name_list = '';
		if($area_ids){
			$where['id'] = array('in',$area_ids);
			$list = $this->where($where)->cache(true)->select();
			foreach ($list as $rs) {
				$area_name = $rs['name'];
				$parent_id = $rs['parentid'];
				if($is_parent && $parent_id > 0){
					$parent_name = $this->id2names($parent_id);
					$area_name = $parent_name . $area_name;
				}
				$area_name_list[] = $area_name;
			}
		}
		$name = implode(',', $area_name_list);
		return $name ? $name : $empty_name;
	}
	/**
	 * 获取区域列表
	 * @param  string $type 级别：0国家，1省份，2城市，3地区
	 */
	public function get_list($type=''){
		if($type){
			$where['type'] = $type;
		}
		$where['is_open'] = 1;
		$list = D('Area')->field('id city_id,name,parentid,is_open')
						 ->order('order_id,id')
						 ->cache(true)
						 ->where($where)
						 ->select();
		return $list;
	}
	public function get_id_name_list($type=''){
		$list = $this->get_list($type);
		foreach ($list as $rs) {
			$area_id_name[$rs['city_id']] = $rs['name'];
		}
		return $area_id_name;
	}
	/**
	 * 获取上级信息
	 * @param  [type] $parentid [description]
	 * @return [type]           [description]
	 */
	public function get_parent_info($parentid){
		if(!$parentid){
			return (new stdClass);
		}
		$where['id'] = $parentid;
		$data = D('Area')->field('id,parentid,name,is_open')->where($where)->find();
		if($data){
			import("ORG.Util.Pinyin");
			$py = new PinYin();
			$letter = $py->getAllPY($data['name']);
			$data['city_id']       = $data['id'];
			$data['letter_all']    = $letter;
			$data['letter_simple'] = strtoupper($py->getFirstPY($data['name']));
			$data['parent_name']   = D('Area')->id2names($data['parentid']);
			$data['letter_first']  = substr($letter, 0,1);
		}else{
			$data = (new stdClass);
		}
		return $data;
	}
	/**
	 * 获取热门城市
	 * @return [type] [description]
	 */
	public function get_hot_city(){
		$area_id_name = $this->get_id_name_list(1);
		//热门城市
		$where['type']   = 2;
		$where['is_hot'] = 1;
		$list = D('Area')->field('id city_id,name,parentid,is_open')->where($where)->order('order_id,id')->select();
		$data = array();
		import("ORG.Util.Pinyin");
		$py = new PinYin();
		foreach ($list as $rs) {
			$letter = $py->getAllPY($rs['name']);
			$letter = strtoupper($letter);
			$rs['letter_all']    = $letter;
			$rs['letter_simple'] = strtoupper($py->getFirstPY($rs['name']));
			$rs['parent_name']   = D('Area')->id2names($rs['parentid']);
			$rs['is_open']       = $rs['is_open'] == 1;

			$data[] = $rs;
		}
		return $data;
	}
	/**
	 * 获取开通城市
	 * @return [type] [description]
	 */
	public function get_open_city($limit_num=''){
		$area_id_name = $this->get_id_name_list(1);
		
		//热门城市
		$where['type']    = 2;
		$where['is_open'] = 1;
		$list = D('Area')->field('id city_id,name,parentid')->where($where)->order('order_id,id')->limit($limit_num)->select();
		$data = array();
		foreach ($list as $rs) {
			$parent_name = $area_id_name[$rs['parentid']];
			$rs['parent_name'] = $parent_name?$parent_name:'';
			//APP，去掉厦门
			// $app_type = $_SERVER['HTTP_APP_TYPE'];
			// $app_type = $app_type ? $app_type : 1;
			// if(in_array($app_type, array(2)) && $rs['city_id'] == '60'){
			// 	continue;
			// }
			$data[] = $rs;
		}
		return $data;
	}
	
	/**
	 * 获取父ID
	 * @param  [type] $area_id 地区ID
	 * @return [type]          [description]
	 */
	public function get_parent_id($area_id){
		$where['id'] = $area_id;
		$parent_id = D('Area')->where($where)->getField('parentid');
		return $parent_id?$parent_id:'';
	}
	/**
	 * 获取上级名称
	 * @param  [type] $area_id 地区ID
	 */
	public function get_parent_name($area_id){
		$where['id'] = $this->get_parent_id($area_id);
		$name = D('Area')->where($where)->getField('name');
		return $name?$name:'';
	}
	/**
	 * 获取下级ID
	 * @param  [type] $parentid 父ID
	 */
	public function get_lower_level_id($parentid){
		$where['parentid'] = $parentid;
		$level_id = D('Area')->where($where)->getField('id',true);
		return $level_id?$level_id:'';
	}
	/**
	 * IP地址转城市信息
	 * @param  string $ip IP地址
	 */
	public function ip2city($ip = '', $is_open = false){
		$ip = $ip ? $ip : get_client_ip();
		// $ip       = '47.153.191.255';//加拿大
		// $ip       = '27.159.112.178';//厦门IP
		// $ip       = '114.80.166.240';//上海IP
		// $ip       = '218.66.159.101';//泉州IP
		//获取IP城市名称
		$ipLib    = new \Org\Net\IpLocation('QQWry-GBK.dat');
		$location = $ipLib->getlocation($ip); // 获取IP地址所在的位置
		$country_name = $location['country'];
		$country_name = iconv('gbk', 'utf8', $country_name);

		$str = str_replace(array('本机地址', '市', '局域网', 'IANA', '未知'), '', $country_name);
		$str = explode('省', $str);

		if(count($str) > 1){
			$ip_city_name = $str[1];
		}else{
			$ip_city_name = $str[0];
		}

		$city_info = '';
		if($ip_city_name){
			if($is_open){
				$where['is_open'] = 1;
			}
			$where['name'] = array('like', '%' . $ip_city_name . '%');
			$where['type'] = 2;//城市
			$city_info = D('Area')->field('id city_id,name city_name,parentid province_id')->where($where)->cache(true)->find();
			if($city_info){
				$city_info['province_name'] = $this->id2names($city_info['province_id']);
			}
		}

		if(!$city_info){
			$city_info['city_name']     = '北京';
			$city_info['city_id']       = 52;
			$city_info['province_name'] = '北京';
			$city_info['province_id']   = 2;
		}

		return $city_info;
	}
}
?>